package com.neu.carbon.mes.controller;

import java.util.List;

import com.neu.carbon.mes.domain.*;
import com.neu.carbon.mes.service.*;
import com.neu.carbon.scm.service.IScmSaleOrderService;
import com.neu.carbon.wms.domain.WmsMaterialInfo;
import com.neu.carbon.wms.service.IWmsMaterialInfoService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.github.xiaoymin.knife4j.annotations.DynamicParameter;
import com.github.xiaoymin.knife4j.annotations.DynamicResponseParameters;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.neu.common.annotation.Log;
import com.neu.common.core.controller.BaseController;
import com.neu.common.core.domain.AjaxResult;
import com.neu.common.enums.BusinessType;
import com.neu.common.utils.poi.ExcelUtil;
import com.neu.common.core.page.TableDataInfo;

import javax.annotation.Resource;

/**
 * 计划排产Controller
 * 
 * @author neuedu
 * @date 2024-08-12
 */
@Api(tags = {"计划排产"})
@RestController
@RequestMapping("/mesPlan/productSchedule/productSchedule")
public class MesProductScheduleController extends BaseController
{
    @Autowired
    private IMesProductScheduleService mesProductScheduleService;
    @Resource
    private IWmsMaterialInfoService iWmsMaterialInfoService;
    @Resource
    private IMesProductBomService iMesProductBomService;
    @Resource
    private IMesFactoryModelService iMesFactoryModelService;
    @Resource
    private IMesProcessModelService iMesProcessModelService;
    @Resource
    private IMesProjectService iMesProjectService;
    @Resource
    private IMesProductPlanService iMesProductPlanService;
    @Resource
    private IScmSaleOrderService iScmSaleOrderService;


    /**
     * 查询计划排产列表
     */
    @GetMapping("/list")
    @ApiOperation("查询计划排产列表")
    @DynamicResponseParameters(properties = {
	        @DynamicParameter(name = "total", value = "总记录数"),
            @DynamicParameter(name = "code", value = "状态码，200正确，其他错误"),
            @DynamicParameter(name = "rows", value = "返回业务数据（数组类型）", dataTypeClass = MesProductSchedule.class),
            @DynamicParameter(name = "msg", value = "返回消息内容")
    })
    public TableDataInfo list(MesProductSchedule mesProductSchedule)
    {
        startPage();
        List<MesProductSchedule> list = mesProductScheduleService.selectMesProductScheduleList(mesProductSchedule);
        list.forEach(model->{
            WmsMaterialInfo wmsMaterialInfo = iWmsMaterialInfoService.selectWmsMaterialInfoById(model.getMaterialId());
            if (wmsMaterialInfo!=null){
                model.setMaterialName(wmsMaterialInfo.getName());
                model.setModel(wmsMaterialInfo.getModel());
                model.setSpecification(wmsMaterialInfo.getSpecification());
                model.setUnit(wmsMaterialInfo.getUnit());
            }
            MesProductBom mesProductBom = iMesProductBomService.selectMesProductBomById(model.getBomId());
            if (mesProductBom!=null){
                model.setBomName(mesProductBom.getName());
            }
            MesFactoryModel mesFactoryModel = iMesFactoryModelService.selectMesFactoryModelById(model.getProductLineId());
            if (mesFactoryModel!=null){
                model.setProductLineName(mesFactoryModel.getProductLineName());
            }
            MesProcessModel mesProcessModel = iMesProcessModelService.selectMesProcessModelById(model.getProcessId());
            if (mesProcessModel!=null){
                model.setProcessName(mesProcessModel.getProcessName());
            }
            MesProject mesProject = iMesProjectService.selectMesProjectById(model.getProjectId());
            if(mesProject!=null){
                model.setProjectName(mesProject.getName());
            }
            MesProductPlan mesProductPlan = iMesProductPlanService.selectMesProductPlanById(model.getPlanId());
            if (mesProductPlan!=null){
                model.setPlanName(mesProductPlan.getSerialNo());
            }
//            ScmSaleOrder scmSaleOrder = iScmSaleOrderService.selectScmSaleOrderById(model.getId());
//            System.out.println(scmSaleOrder);
//            if (scmSaleOrder!=null){
//                String orderNo = scmSaleOrder.getOrderNo();
//                model.setOrderName(orderNo);
//                System.out.println(orderNo);
//            }
        });
        return getDataTable(list);
    }

    /**
     * 导出计划排产列表
     */
    @ApiOperation("导出计划排产列表")
    @DynamicResponseParameters(properties = {
            @DynamicParameter(name = "code", value = "状态码，200正确，其他错误"),
            @DynamicParameter(name = "msg", value = "文件名")
    })
    @PreAuthorize("@ss.hasPermi('mesPlan/productSchedule:productSchedule:export')")
    @Log(title = "计划排产", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(MesProductSchedule mesProductSchedule)
    {
        List<MesProductSchedule> list = mesProductScheduleService.selectMesProductScheduleList(mesProductSchedule);
        ExcelUtil<MesProductSchedule> util = new ExcelUtil<MesProductSchedule>(MesProductSchedule.class);
        return util.exportExcel(list, "productSchedule");
    }

    /**
     * 获取计划排产详细信息
     */
    @ApiOperation("获取计划排产详细信息")
    @DynamicResponseParameters(properties = {
            @DynamicParameter(name = "code", value = "状态码，200正确，其他错误"),
            @DynamicParameter(name = "data", value = "返回业务数据", dataTypeClass = MesProductSchedule.class),
            @DynamicParameter(name = "msg", value = "返回消息内容")
    })
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {  MesProductSchedule model = mesProductScheduleService.selectMesProductScheduleById(id);
        WmsMaterialInfo wmsMaterialInfo = iWmsMaterialInfoService.selectWmsMaterialInfoById(model.getMaterialId());
        if (wmsMaterialInfo!=null){
            model.setMaterialName(wmsMaterialInfo.getName());
            model.setModel(wmsMaterialInfo.getModel());
            model.setSpecification(wmsMaterialInfo.getSpecification());
            model.setUnit(wmsMaterialInfo.getUnit());

            List<MesProductScheduleMaterial> mesProductScheduleMaterialList = model.getMesProductScheduleMaterialList();
            mesProductScheduleMaterialList.forEach(item->{

                if (item.getMaterialId().equals(wmsMaterialInfo.getId())){
                    item.setMaterialName(wmsMaterialInfo.getName());
                    item.setModel(wmsMaterialInfo.getModel());
                    item.setSpecification(wmsMaterialInfo.getSpecification());
                    item.setUnit(wmsMaterialInfo.getUnit());
                }
            });
        }

        MesProductBom mesProductBom = iMesProductBomService.selectMesProductBomById(model.getBomId());
        if (mesProductBom!=null){
            model.setBomName(mesProductBom.getName());
        }
        MesFactoryModel mesFactoryModel = iMesFactoryModelService.selectMesFactoryModelById(model.getProductLineId());
        if (mesFactoryModel!=null){
            model.setProductLineName(mesFactoryModel.getProductLineName());
        }
        MesProcessModel mesProcessModel = iMesProcessModelService.selectMesProcessModelById(model.getProcessId());
        if (mesProcessModel!=null){
            model.setProcessName(mesProcessModel.getProcessName());
        }
        MesProject mesProject = iMesProjectService.selectMesProjectById(model.getProjectId());
        if(mesProject!=null){
            model.setProjectName(mesProject.getName());
        }
        MesProductPlan mesProductPlan = iMesProductPlanService.selectMesProductPlanById(model.getPlanId());
        if (mesProductPlan!=null){
            model.setPlanName(mesProductPlan.getSerialNo());
        }
//        ScmSaleOrder scmSaleOrder = iScmSaleOrderService.selectScmSaleOrderById(model.getId());
//        System.out.println(scmSaleOrder);
//        if (scmSaleOrder!=null){
//            String orderNo = scmSaleOrder.getOrderNo();
//            model.setOrderName(orderNo);
//            System.out.println(orderNo);
//        }

        System.out.println(model);
        return AjaxResult.success(model); }

    /**
     * 新增计划排产
     */
    @ApiOperation("新增计划排产")
    @DynamicResponseParameters(properties = {
            @DynamicParameter(name = "code", value = "状态码，200正确，其他错误"),
            @DynamicParameter(name = "msg", value = "返回消息内容")
    })
    @PreAuthorize("@ss.hasPermi('mesPlan/productSchedule:productSchedule:add')")
    @Log(title = "计划排产", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MesProductSchedule mesProductSchedule)
    {
        return toAjax(mesProductScheduleService.insertMesProductSchedule(mesProductSchedule));
    }

    /**
     * 修改计划排产
     */
    @ApiOperation("修改计划排产")
    @DynamicResponseParameters(properties = {
            @DynamicParameter(name = "code", value = "状态码，200正确，其他错误"),
            @DynamicParameter(name = "msg", value = "返回消息内容")
    })
    @PreAuthorize("@ss.hasPermi('mesPlan/productSchedule:productSchedule:edit')")
    @Log(title = "计划排产", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MesProductSchedule mesProductSchedule)
    {
        return toAjax(mesProductScheduleService.updateMesProductSchedule(mesProductSchedule));
    }

    /**
     * 删除计划排产
     */
    @ApiOperation("删除计划排产")
    @DynamicResponseParameters(properties = {
            @DynamicParameter(name = "code", value = "状态码，200正确，其他错误"),
            @DynamicParameter(name = "msg", value = "返回消息内容")
    })
    @PreAuthorize("@ss.hasPermi('mesPlan/productSchedule:productSchedule:remove')")
    @Log(title = "计划排产", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(mesProductScheduleService.deleteMesProductScheduleByIds(ids));
    }
}
